package me.xplabs.model.proxy 
{
	import flash.geom.Point;
	import flash.utils.getQualifiedClassName;
	import me.xplabs.engine.time.TimeManager;
	import me.xplabs.Globals;
	import me.xplabs.model.energy.EnergyData;
	import me.xplabs.model.energy.EnergyWaveData;
	import me.xplabs.model.msg.MsgChangeRaidInfo;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.msg.NotificationRaidInfo;
	import me.xplabs.utils.SimpleObjPool;
	import me.xplabs.utils.Utils;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	/**
	 * 关卡敌人出怪波数数据控制
	 * http://www.xplabs.me/
	 * @author xiaohan
	 */
	public class EnergyWaveProxy extends Proxy 
	{
		public static var NAME:String;
		private var _waveNumbers:Vector.<EnergyWaveData>;
		private var _isFight:Boolean;
		private var _curTime:int;
		private var _durationKey:int;
		private var _gapTimeKey:int;
		private var curWave:int;
		private var totalWave:int;
		public var gold:int;
		public var life:int;
		private var _curEnergyWaveData:EnergyWaveData;
		
		private var _energyWavePool:SimpleObjPool;
		private var _energyDataPool:SimpleObjPool;
		private var _isSendEndWave:Boolean;
		private var _timeManager:TimeManager;
		private var _restar:Boolean;
		public function EnergyWaveProxy() 
		{
			NAME = getQualifiedClassName(this);
			super(NAME, null);
			_durationKey = -1;
			_gapTimeKey = -1;
			_curTime = 0;
			curWave = 0;
			_waveNumbers = new Vector.<EnergyWaveData>();
			_energyWavePool = new SimpleObjPool(EnergyWaveData, 1, 1);
			_energyDataPool = new SimpleObjPool(EnergyData, 1, 1);
			//_timeManager = new TimeManager();
			//Globals.gameManager.addDataUpdate(onEnterFrame);
			
			_timeManager = Globals.suspendTime;
		}
		
		
		//private function onEnterFrame():void 
		//{
			//_timeManager.update();
		//}
		/**
		 * 初始化敌人波数数据
		 * @param	xml
		 */
		public function updateEnergyData(xml:XML):void
		{
			_restar = true;
			disposeEnergyData();
			_restar = false;
			totalWave = xml.total;
			gold = xml.gold;
			life = xml.life;
			sendNotification(NotificationName.UPDATE_RAIDINFO, new MsgChangeRaidInfo(xml.life, xml.gold, curWave, xml.total), NotificationRaidInfo.INIT_DATE_RAIDINFO);
			trace("总波数:" + totalWave);
			for each (var waveNumber:XML in xml.waveNumber) 
			{
				var tempEnergyWave:EnergyWaveData = _energyWavePool.getObj();
				//var tempEnergyWave:EnergyWaveData = new EnergyWaveData();
				tempEnergyWave.waveNumber = waveNumber.@num;
				tempEnergyWave.duration = waveNumber.duration;
				for each (var energy:XML in waveNumber.energy) 
				{
					//var tempEnergyData:EnergyData = new EnergyData();
					var tempEnergyData:EnergyData = _energyDataPool.getObj();
					var startPosAry:Array = String(energy.start).split("-");
					var endPosAry:Array = String(energy.end).split("-");
					tempEnergyData.startPos = new Point(startPosAry[0], startPosAry[1]);
					tempEnergyData.endPos = new Point(endPosAry[0], endPosAry[1]);
					tempEnergyData.energyId = energy.id;
					tempEnergyData.energyNumber = energy.number;
					tempEnergyWave.energys.push(tempEnergyData);
				}
				_waveNumbers.push(tempEnergyWave);
			}
			_timeManager.addCd("delayStart", 10, null, function():void
			{
				nextWaveEnergy();
			});
			//nextWaveEnergy();
		}
		public function disposeEnergyData():void
		{
			var len:int = _waveNumbers.length;
			for (var i:int = 0; i < len; i++) 
			{
				var tLen:int = _waveNumbers[i].energys.length;
				for (var j:int = 0; j < tLen; j++) 
				{
					_waveNumbers[i].energys[j].dispose();
					_energyDataPool.dispose(_waveNumbers[i].energys[j]);
				}
				_waveNumbers[i].dispose();
				_energyWavePool.dispose(_waveNumbers[i]);
			}
			
			if (_gapTimeKey != -1)
			{
				var temp:int = _gapTimeKey;
				_gapTimeKey = -1;
				_timeManager.removeTimePiece(temp);
			}
			
			if (_durationKey != -1) 
			{
				temp = _durationKey;
				_durationKey = -1;
				_timeManager.removeTimePiece(temp);
			}
			
			_waveNumbers.length = 0;
			_curTime = 0;
			curWave = 0;
			_isSendEndWave = false;
			
		}
		public function startFight(p_energyWave:EnergyWaveData):void
		{
			_curEnergyWaveData = p_energyWave;
			if (_durationKey != -1) return;
			_durationKey = _timeManager.addTimePiece(1, update, p_energyWave.duration, endUpdate);
		}
		
		private function update(repeat:int):void 
		{
			print("持续时间：" + repeat);
			var len:int = _curEnergyWaveData.energys.length;
			for (var i:int = 0; i < len; i++) 
			{
				var tempData:EnergyData = _curEnergyWaveData.energys[i];
				if (tempData.appearNumber >= tempData.energyNumber) continue;
				tempData.appearNumber++;
				sendMsg(NotificationName.FIGHT_MAP_MANAGER, _curEnergyWaveData.energys[i], NotificationFight.APPEAR_ENERGY);
			}
			if (curWave >= totalWave)
			{
				var isAllEnergy:Boolean = true;
				for (i = 0; i < len; i++)
				{
					tempData = _curEnergyWaveData.energys[i];
					if (tempData.appearNumber < tempData.energyNumber)
					{
						isAllEnergy = false;
					}
				}
				if (isAllEnergy && !_isSendEndWave)
				{
					_isSendEndWave = true;
					print("最后一波敌人，并且所有的敌人都已经出来！！！");
					sendMsg(NotificationName.FIGHT_MAP_MANAGER, null, NotificationFight.LAST_ENERGY_WAVE);
				}
			}
		}
		
		private function endUpdate(p_key:int):void 
		{
			if (_restar) return;
			_isFight = false;
			_durationKey = -1;
			if (curWave >= totalWave)
			{
				print("已经到最后一波,等待游戏结束!");
				return;
			}
			_gapTimeKey = _timeManager.addTimePiece(1, updateGapHandler, 10, endGapHandler);
		}
		
		private function endGapHandler(p_key:int):void 
		{
			if (_restar) return;
			if (_gapTimeKey == -1) return;
			_gapTimeKey = -1;
			nextWaveEnergy();
		}
		
		private function updateGapHandler(repeat:int):void 
		{
		}
		
		public function nextWaveEnergy():void
		{
			if (_durationKey != -1) return;
			if (_gapTimeKey != -1)
			{
				var temp:int = _gapTimeKey;
				_gapTimeKey = -1;
				_timeManager.removeTimePiece(temp);
			}
			curWave++;
			sendNotification(NotificationName.UPDATE_RAIDINFO, curWave, NotificationRaidInfo.UPDATE_CURWAVE);
			var tempEnergyWave:EnergyWaveData = getEnergyWave(curWave);
			startFight(tempEnergyWave);
			
		}
		private function getEnergyWave(p_waveNumber:int):EnergyWaveData
		{
			var len:int = _waveNumbers.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_waveNumbers[i].waveNumber == p_waveNumber)
				{
					return _waveNumbers[i];
				}
			}
			return null;
		}
		
	}
}